package com.example.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.model.acl.pojos.Acl_user;
import com.example.model.user.dtos.LoginUser;
import com.example.model.user.pojos.ApUser;
import com.example.user.mapper.AclUserMapper;
import com.example.user.mapper.ApUserMapper;
import com.example.user.service.AclPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service("userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private AclUserMapper aclUserMapper;

    @Autowired
    private AclPermissionService aclPermissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<Acl_user> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Acl_user::getName,username);
        Acl_user acl_user = aclUserMapper.selectOne(queryWrapper);
        acl_user.setPassword(acl_user.getPassword());
        System.out.println(acl_user.toString());
        if(Objects.isNull(acl_user)){
            throw new RuntimeException("用户名或者密码错误");
        }
        List<String> permissionList = aclPermissionService.selectPermissionValueByUserId(acl_user.getId());
        System.out.println("permissionList "+permissionList.toString());
        return new LoginUser(acl_user,permissionList);
    }
}
